package cn.neusoft.utils;

import org.apache.commons.beanutils.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 日期转换类
 */
public class DateConverter implements Converter {

    private String pattern;

    public DateConverter(String pattern){
        this.pattern = pattern;
    }

    @Override
    public Object convert(Class aClass, Object o) {
        // o  其实就是前端传过来的日期字符串  2022-09-07
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        try {
            Date date = format.parse(o.toString());
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}
